﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using System.Data;

namespace Tireguit.Nmon.Parser.Common
{
    public class NmonElement:INmonElement
    {
        public System.Data.IDbConnection Connection { get; set; }
        public System.Data.IDbCommand Command { get; protected set; }
        public List<System.Data.IDbCommand> ListCommand { get; protected set; }
        protected static char[] separ = { ',' };
        protected static ILog _log = LogManager.GetLogger(typeof(NmonElement));
        protected bool disposed = false;
        protected string _insertreq;
        public System.Data.IDataParameterCollection Parameters { get; protected set; }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (Command!=null)
                    {
                        Command.Dispose();
                        Command = null;
                    }
                }
                disposed = true;
            }
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        ~NmonElement()
        {
            Dispose(false);
        }
        public NmonElement():this(null)
        {
        }
        public NmonElement(IDbConnection connection)
        {
            Balise = BaliseType.UKNOWN;
            Connection = connection;
            _Linestring = string.Empty;
        }
        
        protected string _Linestring;
        public string Linestring
        {
            get { return _Linestring; }
            set { _Linestring = value; }
        }
        public BaliseType Balise { get; protected set; }
        protected virtual bool InternalParse(string line)
        {
            throw new NotImplementedException("InternalParser must be override");
        }
        public virtual List<IDbCommand> GetInsertReq()
        {
            throw new NotImplementedException("GetInsertReq must be override");
        }
       
        public virtual bool Parse(string line)
        {
            try
            {
                return InternalParse(line);
            }
            catch (System.Exception ex)
            {
                _log.Error("Parse", ex);
                return false;
            }
        }
        public static INmonElement Create(BaliseType balise,IDbConnection connection)
        {
            try
            {
                _log.Debug("start Create");
                switch (balise)
                {
                case BaliseType.CPU_ALL:
                        return new Statisitic.CPU_ALLElement(connection);
                case BaliseType.CPU:
                        return new Statisitic.CPUElement(connection);
                case BaliseType.MEM:
                        return new Statisitic.MEMElement(connection);
                case BaliseType.MEMUSE:
                        return new Statisitic.MEMUSEElement(connection);
                case BaliseType.PAGE:
                        return new Statisitic.PAGEElement(connection);
                case BaliseType.PROC:
                        return new Statisitic.PROCElement(connection);
                case BaliseType.FILE:
                        return new Statisitic.FILEElement(connection);
                case BaliseType.TOP:
                        return new Statisitic.TOPElement(connection);
                case BaliseType.NET:
                        return new Statisitic.NETElement(connection);
                case BaliseType.NETPACKET:
                        return new Statisitic.NETPACKETElement(connection);
                case BaliseType.NETERROR:
                    return new Statisitic.NETERRORElement(connection);
                case BaliseType.DISKBSIZE:
                        return new Statisitic.DISKBSIZEElement(connection);
                case BaliseType.DISKBUSY:
                        return new Statisitic.DISKBUSYElement(connection);
                case BaliseType.DISKREAD:
                        return new Statisitic.DISKREADElement(connection);
                case BaliseType.DISKWRITE:
                        return new Statisitic.DISKWRITEElement(connection);
                case BaliseType.DISKXFER:
                        return new Statisitic.DISKXFERElement(connection);
                case BaliseType.IOADAPT:
                        return new Statisitic.IOADAPTElement(connection);
                case BaliseType.JFSFILE:
                        return new Statisitic.JFSFILEElement(connection);
                case BaliseType.JFSINODE:
                        return new Statisitic.JFSINODEElement(connection);
                case BaliseType.ZZZZ:
                        return new Statisitic.ZZZZElement(connection);
                case BaliseType.AAA:
                        return new Information.AAAElement(connection);
                case BaliseType.BBBB:
                    return new Information.BBBBElement(connection);
                case BaliseType.BBBC:
                    return new Information.BBBCElement(connection);
                case BaliseType.BBBD:
                    return new Information.BBBDElement(connection);
                case BaliseType.BBBP:
                    return new Information.BBBPElement(connection);
                
                
                default:
                        return null;
                }
            }
            catch (System.Exception ex)
            {
                _log.Error("Create", ex);
                return null;
            }
            finally
            {
                _log.Debug("stop Create");
            }
        }
        public static BaliseType GetBaliseElement(string line)
        {
            try
            {
                _log.Debug("Start GetBaliseElement");
                /*
                 * UKNOWN=-1,
                    AAA = 0,
                    BBBB =1 ,
                    BBBC =2,
                 * CPU_ALL=3,
                    CPU=4,
                    MEM =5,
                    MEMEUSE=6,
                    PAGE=7,
                    PROC=8,
                    FILE=9,
                    NETPACKET=10,
                    NET=11,
                    DISKBUSY=12,
                    DISKREAD=13,
                    DISKWRITE=14,
                    DISKXFER=15,
                    DISKBSIZE=16,
                    IOADAPT=17,
                    BBBD=18,
                    JFSFILE=11,
                    JFSINODE=12,
                    TOP=13,
                    TOP2=14,//A voir difference top
                    BBBP=15,
                    ZZZZ=16
                 * */
                if (string.IsNullOrEmpty(line))
                {
                    _log.Info("Empty Line");
                    return BaliseType.UKNOWN;
                }
               
                if (line.StartsWith("CPU_ALL,"))
                {
                    return BaliseType.CPU_ALL;
                }
                if (line.StartsWith("CPU"))
                {
                    return BaliseType.CPU;
                }
                if (line.StartsWith("MEMUSE,"))
                {
                    return BaliseType.MEMUSE;
                }
                if (line.StartsWith("MEM,"))
                {
                    return BaliseType.MEM;
                }
                if (line.StartsWith("PAGE,"))
                {
                    return BaliseType.PAGE;
                }
                if (line.StartsWith("PROC,"))
                {
                    return BaliseType.PROC;
                }
                if (line.StartsWith("FILE,"))
                {
                    return BaliseType.FILE;
                }
                if (line.StartsWith("NETPACKET,"))
                {
                    return BaliseType.NETPACKET;
                }
                if (line.StartsWith("NETERROR,"))
                {
                    return BaliseType.NETERROR;
                }
                if (line.StartsWith("NET,"))
                {
                    return BaliseType.NET;
                }
                if (line.StartsWith("DISKBUSY,"))
                {
                    return BaliseType.DISKBUSY;
                }
                if (line.StartsWith("DISKREAD,"))
                {
                    return BaliseType.DISKREAD;
                }
                if (line.StartsWith("DISKWRITE,"))
                {
                    return BaliseType.DISKWRITE;
                }
                if (line.StartsWith("DISKXFER,"))
                {
                    return BaliseType.DISKXFER;
                }
                if (line.StartsWith("DISKBSIZE,"))
                {
                    return BaliseType.DISKBSIZE;
                }
                if (line.StartsWith("IOADAPT,"))
                {
                    return BaliseType.IOADAPT;
                }
                
                if (line.StartsWith("JFSFILE,"))
                {
                    return BaliseType.JFSFILE;
                }
                if (line.StartsWith("JFSINODE,"))
                {
                    return BaliseType.JFSINODE;
                }
                if (line.StartsWith("TOP,"))
                {
                    return BaliseType.TOP;
                }
                
                if (line.StartsWith("ZZZZ,"))
                {
                    return BaliseType.ZZZZ;
                }
                if (line.StartsWith("AAA,"))
                {
                    return BaliseType.AAA;
                }
                if (line.StartsWith("BBBB,"))
                {
                    return BaliseType.BBBB;
                }
                if (line.StartsWith("BBBC,"))
                {
                    return BaliseType.BBBC;
                }
                if (line.StartsWith("BBBD,"))
                {
                    return BaliseType.BBBD;
                }
                if (line.StartsWith("BBBP,"))
                {
                    return BaliseType.BBBP;
                }
                _log.Warn("Unrecognized line:" + line);
                return BaliseType.UKNOWN;
            }
            catch (System.Exception ex)
            {
                _log.Error("GetBaliseElement", ex);
                return BaliseType.UKNOWN;
            }
            finally
            {
                _log.Debug("Stop GetBaliseElement");
            }
        }
    }
}
